package org.com.android.db;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Schema {
	private DBOpenHelper helper;
	
	private final String[] createtables = new String[]{
		"CREATE TABLE IF NOT EXISTS good  (_id VARCHAR(64) PRIMARY KEY, status INT)",
		"CREATE TABLE IF NOT EXISTS entity (_id VARCHAR(64) PRIMARY KEY, name VARCHAR(64))"
	} ;
	
	private final String[] droptables = new String []{
		"DROP TABLE IF EXISTS good",
		"DROP TABLE IF EXISTS entity"
	} ;
	
	public Schema(Context c) {
		if (helper == null) { 
			helper = new DBOpenHelper(c);
		}
	}

	public Itable gettable(String tablename) {		
		if(tablename == Good.name){
			return new Good(this) ;
		}
		return null ;
	}
	
	public SQLiteDatabase getdatabaseforread(){
		return helper.getReadableDatabase() ;
	}
	
	public SQLiteDatabase getdatabaseforwrite(){
		return helper.getWritableDatabase();
	}

 	private class DBOpenHelper extends SQLiteOpenHelper {
		private static final String DBNAME = "polo.db";
		private static final int VERSION = 2;

		public DBOpenHelper(Context context) {
			super(context, DBNAME, null, VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			try {
				for(String sql : createtables){
					db.execSQL(sql) ;
				}
			}catch(SQLException e){
				Log.e("Schema::DBOpenHelper::OnCreate", e.getMessage()); 
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			try {
				for (String sql : droptables){
					db.execSQL(sql);
				}				
			} catch(SQLException e){
				Log.e("Schema::DBOpenHelper::onUpgrade",e.getMessage());
			}
			onCreate(db);
		}
	}

}